package cn.com.hhrcw.resumeDeliveryConfig.web;

import cn.com.hhrcw.entity.SsSession;
import cn.com.hhrcw.resumeDeliveryConfig.entity.ResumeDeliveryConfig;
import cn.com.hhrcw.resumeDeliveryConfig.mapstruct.ResumeDeliveryConfigConvert;
import cn.com.hhrcw.resumeDeliveryConfig.params.ResumeDeliveryConfigParams;
import cn.com.hhrcw.resumeDeliveryConfig.service.IResumeDeliveryConfigService;
import cn.com.hhrcw.resumeDeliveryConfig.vo.ConfigVO;
import cn.com.hhrcw.resumeDeliveryConfig.vo.ResumeDeliveryConfigVO;

import cn.com.hhrcw.service.ISsSessionService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.base.entity.AttributesValueParams;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
*
* @module zyzg-boot-user
* @author shengli
* @since 2021-12-27
*/
@RestController
@Slf4j
@RequestMapping("/admin/resumeDeliveryConfig")
@Validated
public class ResumeDeliveryConfigController extends JeecgController<ResumeDeliveryConfig, IResumeDeliveryConfigService> {

    @Autowired
    private IResumeDeliveryConfigService resumeDeliveryConfigService;
    @Autowired
    private ISsSessionService ssSessionService;

    @Autowired
    private ResumeDeliveryConfigConvert resumeDeliveryConfigConvert;


    /**
    * 分页列表查询
    *
    * @param params 请求参数
    * @return
    */
    @GetMapping(value = "/list")
    @AutoLog("-分页列表查询")
    public Result<Page<ResumeDeliveryConfigVO>> list(ResumeDeliveryConfigParams params, HttpServletRequest request) {
        QueryWrapper<ResumeDeliveryConfig> queryWrapper = QueryGenerator.initQueryWrapper(resumeDeliveryConfigConvert.toEntity(params), request.getParameterMap());
        IPage<ResumeDeliveryConfig> page = resumeDeliveryConfigService.page(params.page(), queryWrapper);
        //分页结果转换器
        Function<ResumeDeliveryConfig, ResumeDeliveryConfigVO> fun = resumeDeliveryConfig -> resumeDeliveryConfigConvert.toVO(resumeDeliveryConfig);
        return Result.ok((Page)page.convert(fun));
    }

    /**
    * 根据id查询
    *
    * @param params 数据id
    * @return
    */
    @GetMapping(value = "/queryById")
    public Result<ResumeDeliveryConfigVO> queryById(ResumeDeliveryConfigParams params) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Assert.isFalse(StrUtil.isBlank(params.getObjId()) || StrUtil.isBlank(params.getConfigType()), "参数异常！");
        String sess = "special_session";
        if (sess.equals(params.getConfigType())) {
            SsSession ssSession = ssSessionService.getById(params.getObjId());
            Assert.notNull(ssSession, "数据异常！");
        }

        ResumeDeliveryConfig resumeDeliveryConfig = resumeDeliveryConfigService
            .lambdaQuery()
            .eq(ResumeDeliveryConfig::getObjId,params.getObjId())
            .one();
        if (null == resumeDeliveryConfig) {
            resumeDeliveryConfig = new ResumeDeliveryConfig();
            resumeDeliveryConfig.setObjId(params.getObjId());
            resumeDeliveryConfig.setConfigType(params.getConfigType());
            resumeDeliveryConfig.setCreateBy(sysUser.getId());
            resumeDeliveryConfig.setCreateTime(new Date());
            resumeDeliveryConfig.setAttachmentResumeFlag(false);
            ConfigVO configVO = new ConfigVO();
            configVO.setWorkIntentions(false);
            configVO.setUserWorkInfos(false);
            configVO.setUserEduInfos(false);
            configVO.setProjectInfos(false);
            configVO.setTrainInfos(false);
            configVO.setLanguageLevelInfos(false);
            configVO.setSkillsInfos(false);
            configVO.setCertificateInfos(false);
            configVO.setFamilyMembers(false);
            configVO.setFiles(false);
            resumeDeliveryConfig.setConfigJson(JSONUtil.toJsonStr(configVO));
            resumeDeliveryConfig.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
            resumeDeliveryConfigService.save(resumeDeliveryConfig);
        }
        ResumeDeliveryConfigVO resumeDeliveryConfigVO = resumeDeliveryConfigConvert.toVO(resumeDeliveryConfig);
        resumeDeliveryConfigVO.setConfigVO(JSONUtil.toBean(JSONUtil.parseObj(resumeDeliveryConfigVO.getConfigJson()), ConfigVO.class));
        return Result.ok(resumeDeliveryConfigVO);
    }

    /**
    * 添加
    *
    * @param params 请求参数
    * @return
    */
    @PostMapping(value = "/add")
    @AutoLog("-添加")
    public Result<?> add(@RequestBody @Valid ResumeDeliveryConfigParams params) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ResumeDeliveryConfig resumeDeliveryConfig = resumeDeliveryConfigConvert.toEntity(params);
        resumeDeliveryConfig.setCreateBy(sysUser.getUsername());
        resumeDeliveryConfig.setCreateTime(DateUtil.date());

        boolean saved = resumeDeliveryConfigService.save(resumeDeliveryConfig);
        return saved ? Result.ok() : Result.error("添加失败");
    }

    /**
    * 编辑
    *
    * @param params 请求参数
    * @return
    */
    @PostMapping(value = "/edit")
    @AutoLog("-编辑")
    public Result<?> edit(@RequestBody @Valid ResumeDeliveryConfigParams params) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ResumeDeliveryConfig resumeDeliveryConfig = resumeDeliveryConfigConvert.toEntity(params);
        resumeDeliveryConfig.setUpdateBy(sysUser.getId());
        resumeDeliveryConfig.setUpdateTime(DateUtil.date());
        resumeDeliveryConfig.setConfigJson(JSONUtil.toJsonStr(params.getConfigVO()));
        boolean updated = resumeDeliveryConfigService.updateById(resumeDeliveryConfig);
        return updated ? Result.ok() : Result.error("编辑失败");
    }


    /**
    * 批量删除
    *
    * @param id 数据id
    * @return
    */
    @GetMapping(value = "/delete")
    @AutoLog("-批量删除")
    @RequiresPermissions("resumeDeliveryConfig:batch_del")
    public Result delete(String[] id) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaUpdateWrapper<ResumeDeliveryConfig> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(ResumeDeliveryConfig::getId, id)
            .set(ResumeDeliveryConfig::getUpdateBy, sysUser.getUsername())
            .set(ResumeDeliveryConfig::getUpdateTime, DateUtil.date());

        boolean deleted = resumeDeliveryConfigService.update(updateWrapper);
        return deleted ? Result.ok() : Result.error("删除失败");
    }

}
